var socket;  
  
/** 
 * 初始化请求服务器端以建立websocket连接，host为本地localhost,请求的端口号为12345 
 * 之后为socket的各事件绑定事件内容 
 */  
function init(){  
	var host = "ws://localhost/web/home/socket/index/id/72.html";  
	try{  
		socket = new WebSocket(host);  
		log('WebSocket - status '+socket.readyState);  
		socket.onopen    = function(msg){ log("Welcome - status "+this.readyState); };  
		socket.onmessage = function(msg){ log("Received: "+ msg.data); };  
		socket.onclose   = function(msg){ log("Disconnected - status "+this.readyState); };  
	} catch(ex) {  
		log(ex);  
	}  
}  
  
/** 
 * send方法发送消息到服务器端 
 */  
function send(){  
	var msg = $("msg").value;  
	if (!msg) return false;  
	  
	$("msg").value="";  
	try{  
		socket.send(msg);  
		log('Sent: '+msg);  
	} catch(ex) {  
		log(ex);  
	}  
}  

function log(msg) {  
	//$("log").innerHTML+="<br>"+msg;  
	alert(msg);
}  
 